﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace MyEngineExercise.我的扩展
{
    /// <summary>
    /// toc点击事件包装类
    /// </summary>
    internal sealed class TocCtrlClick
    {
        /// <summary>
        /// 初始化，判断控件hit的是什么？如：ItemMap、ItemLayer、ItemHeading、ItemLegendClass
        /// </summary>
        /// <param name="tocCtrl"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        internal static TocCtrlClick Init(ITOCControl2 tocCtrl,Int32 x,Int32 y)
        {
            IBasicMap hitBasicMap = null;
            ILayer hitLayer = null;//hit的图层
            System.Object hitLegendObj = null;//hit的图层的样式
            System.Object hitIndexObj = null;//hit的图层的索引
            esriTOCControlItem hitTocItem = esriTOCControlItem.esriTOCControlItemNone;

            tocCtrl.HitTest(x, y, ref hitTocItem, ref hitBasicMap, ref hitLayer, ref hitLegendObj, ref hitIndexObj);
            
            //判断toc项的类型
            //ItemMap
            IMap map = (Int32)hitTocItem >= (Int32)esriTOCControlItem.esriTOCControlItemMap ? (IMap)hitBasicMap : null;
            //ItemLayer
            ILayer layer = (Int32)hitTocItem >= (Int32)esriTOCControlItem.esriTOCControlItemLayer ? hitLayer : null;
            //ItemHeading
            ILegendGroup legendGroup = (Int32)hitTocItem >= (Int32)esriTOCControlItem.esriTOCControlItemHeading ? (ILegendGroup)hitLegendObj : null;
            //ItemLegendClass
            Int32 index = (Int32)hitTocItem >= (Int32)esriTOCControlItem.esriTOCControlItemLegendClass ? Convert.ToInt32(hitIndexObj.ToString()) : -1;

            return new TocCtrlClick(hitTocItem, map, layer, legendGroup, index, x, y);
        }


        private esriTOCControlItem _tocItem = esriTOCControlItem.esriTOCControlItemNone;
        private IMap _map = null;
        private ILayer _layer = null;
        private ILegendGroup _legendGroup = null;
        private Int32 _index = -1;
        private Int32 _x = -1;
        private Int32 _y = -1;


        public Boolean IsHitLayerItem{get { return _tocItem == esriTOCControlItem.esriTOCControlItemLayer; }}
        public Boolean IsHitShapeLayerItem { get { return IsHitLayerItem && null != FeatureClass && FeatureClass.IsShapeFile(); } }
        public Boolean IsHitMdbLayerItem { get { return IsHitLayerItem && null != FeatureClass && FeatureClass.IsInMdbFile();} }

        public IFeatureLayer FeatureLayer { get { return _layer as IFeatureLayer; } }
        public IFeatureClass FeatureClass { get { return FeatureLayer.FeatureClass; } }
        public ILayer HitLayer { get { return _layer; } }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tocItem"></param>
        /// <param name="map"></param>
        /// <param name="layer"></param>
        /// <param name="legendGroup"></param>
        /// <param name="index"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public TocCtrlClick(
            esriTOCControlItem tocItem,
            IMap map,
            ILayer layer,
            ILegendGroup legendGroup,
            Int32 index,
            Int32 x,
            Int32 y)
        {
            _tocItem = tocItem;
            _map = map;
            _layer = layer;
            _legendGroup = legendGroup;
            _index = index;
            _x = x;
            _y = y;
        }
    }
}
